﻿using System.Windows;
using System.Windows.Controls;

namespace Quiz_Builder.Controls
{
    public class RibbonStackPanel : ItemsControl
    {
        #region Dependency Properties

        public DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof (string),
                                                                               typeof (RibbonStackPanel),
                                                                               new PropertyMetadata("Header",
                                                                                                    HeaderChanged));

        public string Header
        {
            get { return (string) GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }

        private static void HeaderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (d as RibbonStackPanel);
            if (control != null)
                if (control.tbHeader != null)
                    control.tbHeader.Text = e.NewValue.ToString();
        }

        #endregion

        #region Properties

        private TextBlock _tbHeader;

        private TextBlock tbHeader
        {
            get
            {
                if (_tbHeader == null)
                {
                    _tbHeader = GetTemplateChild("tbHeader") as TextBlock;
                }
                return _tbHeader;
            }
        }

        #endregion

        public RibbonStackPanel()
        {
            DefaultStyleKey = typeof (RibbonStackPanel);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (tbHeader != null)
                tbHeader.Text = Header;
        }
    }
}